package com.joolun.tennis.service.impl;

import java.util.List;
import com.joolun.common.utils.DateUtils;
import com.joolun.tennis.domain.CoachClub;
import com.joolun.tennis.mapper.CoachClubMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.joolun.tennis.mapper.CoachMapper;
import com.joolun.tennis.domain.Coach;
import com.joolun.tennis.service.ICoachService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 教练Service业务层处理
 * 
 * @author www.joolun.com
 * @date 2022-03-08
 */
@Service
public class CoachServiceImpl implements ICoachService 
{
    @Autowired
    private CoachMapper coachMapper;

    @Autowired
    private CoachClubMapper coachClubMapper;

    /**
     * 查询教练
     * 
     * @param id 教练ID
     * @return 教练
     */
    @Override
    public Coach selectCoachById(Long id)
    {
        return coachMapper.selectCoachById(id);
    }

    /**
     * 查询教练列表
     * 
     * @param coach 教练
     * @return 教练
     */
    @Override
    public List<Coach> selectCoachList(Coach coach)
    {
        return coachMapper.selectCoachList(coach);
    }

    @Override
    public List<Coach> selectCoachListByClubId(Long clubId) {
        return coachMapper.selectCoachListByClubId(clubId);
    }

    /**
     * 新增教练
     * 
     * @param coach 教练
     * @return 结果
     */
    @Override
    public int insertCoach(Coach coach)
    {
        coach.setCreateTime(DateUtils.getNowDate());
        return coachMapper.insertCoach(coach);
    }

    /**
     * 修改教练
     * 
     * @param coach 教练
     * @return 结果
     */
    @Override
    public int updateCoach(Coach coach)
    {
        coach.setUpdateTime(DateUtils.getNowDate());
        return coachMapper.updateCoach(coach);
    }

    /**
     * 批量删除教练
     * 
     * @param ids 需要删除的教练ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCoachByIds(Long[] ids)
    {
        int rows=coachMapper.deleteCoachByIds(ids);
        coachClubMapper.deleteCoachClubByCoachIds(ids);
        return rows;
    }

    /**
     * 删除教练信息
     * 
     * @param id 教练ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCoachById(Long id)
    {
        //根据coachId删除coach_club关联表数据
        coachMapper.deleteCoachClubByCoachId(id,null);
        return coachMapper.deleteCoachById(id);
    }

    @Override
    public int insertCoachClub(Long coachId, Long clubId) {
        if(coachClubMapper.selectCoachClub(clubId,coachId)>0){
            return 0;
        }
        CoachClub cc = new CoachClub();
        cc.setClubId(clubId);
        cc.setCoachId(coachId);
        return coachClubMapper.insertCoachClub(cc);
    }

    @Override
    public int selectCoachClub(Long clubId, Long coachId) {
        return coachClubMapper.selectCoachClub(clubId,coachId);
    }

    @Override
    public int cancelCoach(Long coachId, Long clubId) {
        return coachClubMapper.deleteCoachClubByCoachId(coachId,clubId);
    }
}
